package com.woniu51.search.controller;

import com.woniu.common.core.controller.BaseController;
import com.woniu.common.core.domain.AjaxResult;
import com.woniu51.search.domain.ActorSearch;
import com.woniu51.search.domain.CinemaSearch;
import com.woniu51.search.domain.MovieSearch;
import com.woniu51.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 杨光
 * @date 2022年04月12日 10:17
 */
@RestController
public class SearchController extends BaseController {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private SearchService searchService;

    @RequestMapping("web/search/searchActorByKey")
    public AjaxResult searchActorByKey(String searchKey){
        List<ActorSearch> list = searchService.searchActorByKey(searchKey);
        return AjaxResult.success("ok",list);
    }
    @RequestMapping("web/search/searchMovieByKey")
    public AjaxResult searchMovieByKey(String searchKey){
        List<MovieSearch> list = searchService.searchMovieByKey(searchKey);
        return AjaxResult.success("ok",list);
    }

    @RequestMapping("web/search/searchCinemaByKey")
    public AjaxResult searchCinemaByKey(String searchKey){
        List<CinemaSearch> list = searchService.searchCinameByKey(searchKey);
        return AjaxResult.success("ok",list);
    }

    @GetMapping("web/index/test")
    public String initEs(){
        elasticsearchTemplate.createIndex(CinemaSearch.class);
        elasticsearchTemplate.putMapping(CinemaSearch.class);
        elasticsearchTemplate.createIndex(ActorSearch.class);
        elasticsearchTemplate.putMapping(ActorSearch.class);
        elasticsearchTemplate.createIndex(MovieSearch.class);
        elasticsearchTemplate.putMapping(MovieSearch.class);

        searchService.initEs();

        return "ok";
    }
}
